﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using WS.EKA.Business.Service;
using WS.EKA.Business.Model;
using WS.EKA.Business.Dal;
using WS.EKA.UI.Business;
using WS.EKA.Utility;
using System.Threading.Tasks;

namespace WS.EKA.Web.UI.Service
{
    /// <summary>
    /// Summary description for IncidentService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class IncidentService : System.Web.Services.WebService
    {
        IService<TB_INCIDENT_TEMP> service = ServiceFactory.CreateService<TB_INCIDENT_TEMP>();

        [WebMethod]
        public void SaveIncident(string Incident_Number, string Event_Source, string Category, string SubCategory, string Business_System_Type,
         string Busynesssystemid, string Description, string Detailed_Decription, string Impact, string Urgency, string Priority, string Employee_Number,
         string Customer_Department, string Customer_Email, string Customer_Name, string Customer_Phone, string Assigned_Group, string Assigned_Group_Id,
         string Assignee, string Assignee_Login_Id, string Resolution, string Resolve_Code, string Resolved_By, Nullable<DateTime> Resolved_Date,
         string Status, string Submitter, Nullable<DateTime> Create_Date, string Last_Modified_By, Nullable<DateTime> Modified_Date, string Closed_By, Nullable<DateTime> Closed_Date)
        {
            Logger.Log("IncidentService Begining...");
            IList<TB_INCIDENT_TEMP> list = new List<TB_INCIDENT_TEMP>();

            try
            {
                list.Add(new TB_INCIDENT_TEMP()
                {
                    Incident_Number = Incident_Number,
                    Event_Source = Event_Source,
                    Category = Category,
                    SubCategory = SubCategory,
                    Business_System_Type = Business_System_Type,
                    Busynesssystemid = Busynesssystemid,
                    Description = Description,
                    Detailed_Decription = Detailed_Decription,
                    Impact = Impact,
                    Urgency = Urgency,
                    Priority = Priority,
                    Employee_Number = Employee_Number,
                    Customer_Department = Customer_Department,
                    Customer_Email = Customer_Email,
                    Customer_Name = Customer_Name,
                    Customer_Phone = Customer_Phone,
                    Assigned_Group = Assigned_Group,
                    Assigned_Group_Id = Assigned_Group_Id,
                    Assignee = Assignee,
                    Assignee_Login_Id = Assignee_Login_Id,
                    Resolution = Resolution,
                    Resolve_Code = Resolve_Code,
                    Resolved_By = Resolved_By,
                    Resolved_Date = Resolved_Date,
                    Status = Status,
                    Submitter = Submitter,
                    Create_Date = Create_Date,
                    Last_Modified_By = Last_Modified_By,
                    Modified_Date = Modified_Date,
                    Closed_By = Closed_By,
                    Closed_Date = Closed_Date
                });
                service.ImportData(list);
            }
            catch (Exception ex)
            {

                Logger.Error("IncidentService Error.", ex);
                throw;
            }
            PushToClient(list);
            Logger.Log("IncidentService succeed!");
        }

        private void PushToClient(IList<TB_INCIDENT_TEMP> list)
        {
            Task.Factory.StartNew(() =>
                   {
                       //工单普通模式
                       ClientDataSourceBridge.PushIncidentOrdinaryMode(list[0].Urgency, list[0].Status, list[0].Create_Date ?? DateTime.Now);
                       ClientDataSourceBridge.PushIncidentLeaderMode();
                   }).ContinueWith(t =>
                   {
                       foreach (var inner in t.Exception.InnerExceptions)
                       {
                           Logger.Error("Incident push Error.", inner);
                       }
                   }, TaskContinuationOptions.OnlyOnFaulted);
        }
    }
}
